from pathlib import Path
import os
import sys
import json

class Student:
    # 初始化可以不分配ID
    def __init__(self,name,group,id=None,additionalInfo=None):
        self.name=name
        self.group=group
        if id==None:
            self.id=0
        else:
            self.id=id
        
        # 还可以存储额外信息，存成字典
        if additionalInfo==None:
            self.addiInfo={}
        else:
            self.addiInfo=additionalInfo
    
    # 指定ID
    def setID(self,id):
        self.id=id

    
    # 按照ID确定学生
    def __eq__(self,other):
        if self.id==other.id:
            return True
        else:
            return False
    # 重载运算符：按照ID排序
    def __lt__(self, other):# 小于
        if self.id < other.id:
            return True
        else:
            return False
    def __le__(self, other):# 小于等于
        if self.id <= other.id:
            return True
        else:
            return False
    def __gt__(self, other):# 大于
        if self.id > other.id:
            return True
        else:
            return False
    def __ge__(self, other):# 大于等于
        if self.id >= other.id:
            return True
        else:
            return False

    # 重载字符串和Show
    def __str__(self):
        return f'学号: {self.id}\t姓名: {self.name}\t小组: {self.group}'
    def show(self):
        print(self)


'''
example: 只根据ID判断，ID相同则相同
stuA=Student("stuA",1,11001)
stuB=Student("stuB",2,11001)
print(stuA)
print(stuB)
print(stuA==stuB)
'''

class StuList:
    def __init__(self):
        self.list=[]

    # 没问题返回True，有错误raise TypeError
    def check(self):
        for stu in self.list:
            if(type(stu)!=Student):
                raise TypeError(f'只有 Student 对象可以加入此列表。')
        return True
    
    # 检查是否有重复，无错误返回True，有重复raise ValueError
    def checkRepeated(self):
        for i in range(0,len(self.list)):
            for j in range(i+1,len(self.list)):
                if self.list[i].id==self.list[j].id:
                    raise ValueError(f'列表中已存在ID相同的学生：{self.list[i].id}')

        return True


    # 添加学生
    def addStudent(self,stu):
        if(type(stu)!=Student):
            raise TypeError('只有 Student 对象可以加入此列表。')
        else:
            self.list.append(stu)
        
        self.checkRepeated()

    # 清空列表
    def clear(self):
        self.list=[]

    # 添加列表到列表
    def appendList(self,stuList):
        for stu in stuList:
            if(type(stu)!=Student):
                raise TypeError('只有 Student 对象可以加入此列表。')
            self.list.append(stu)
        self.check()
        self.checkRepeated()

    # 导入列表并覆盖
    def importList(self,stuList):
        self.list=stuList
        self.check()
        self.checkRepeated()

    # 删除，return True/False 分别表示成功/失败
    # 根据id删除
    def deleteStu_byId(self,id):
        for i in range(len(self.list)):
            if(self.list[i].id==id):
                del self.list[i]
                return True
        return False
    # 根据顺序删除
    def deleteStu_byOrder(self,i):
        if(i<len(self.list)):
            del self.list[i]
            return True
        return False

    # 排序，长度
    def sortList(self):
        self.list.sort()
    def len(self):
        return self.list.len

    # 显示
    def show(self):
        print('ID\t姓名\t小组')
        print('---------------------')
        for stu in self.list:
            print(f'{stu.id}\t{stu.name}\t{stu.group}')

'''
example: 类型必须为 Student
test='string'
lst=StuList()
lst.addStudent(test)

# Test: 排序
import random
lst=StuList()
for i in range(1,6):
    stu=Student(f'stu{i}',1,random.randint(100,400))
    lst.addStudent(stu)

lst.show()
lst.sortList()
lst.show()

students=[]
for i in range(0,8):
    students.append(Student(f'stu{random.randint(1,100)}',2,random.randint(1,100)))

lst.appendList(students)
lst.show()
lst.sortList()

lst.addStudent(Student('stu600',2,600))
lst.show()
lst.deleteStu_byId(600)
lst.show()
'''

# 日志 ClassLog 类
class ClassLog:
    def __init__(self,logDate,logTime,logText,stu):
        self.logDate=logDate
        self.logTime=logTime
        self.logText=logText
        self.stu=stu
